<%--<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>--%>
<%@ page import="com.bdqn.entity.News" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.bdqn.service.newsService" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.io.File" %>
<jsp:useBean id="NewsService" class="com.bdqn.service.Impl.NewsServiceImpl"></jsp:useBean>
<%
    //设置请求时的编码格式
    request.setCharacterEncoding("utf-8");
    //设置响应时的编码类型
    response.setContentType("text/html;charset=utf-8");
    //判断请求类型中是否包含文件
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    News news = new News();
    if (isMultipart){
        //创建文件上传的核心组件
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        //用文件上传核心组件解析请求
        List<FileItem> items = upload.parseRequest(request);
        //用迭代器方式进行循环遍历
        Iterator<FileItem> it = items.iterator();

        while (it.hasNext()){
            //获取遍历到的每一项
            FileItem item=it.next();
            //判断是否有普通表单域
            if (item.isFormField()){
                //获取表单域名称
                String fieldName = item.getFieldName();
                if ("categoryId".equals(fieldName)) {
                    news.setCategoryId(Integer.parseInt(item.getString("UTF-8")));
                }else if ("title".equals(fieldName)){
                    news.setTitle(item.getString("UTF-8"));
                }else if ("author".equals(fieldName)){
                    news.setAuthor(item.getString("UTF-8"));
                }else if ("summary".equals(fieldName)){
                    news.setSummary(item.getString("UTF-8"));
                }else if ("content".equals(fieldName)){
                    news.setContent(item.getString("UTF-8"));
                }
            }else {
                //文件域
                String fileName = item.getName();//获取文件名称
                //确定上传文件的路径
                File saveDirFile = new File(request.getServletContext().getRealPath("static/upload"));
                //判断目录是否存在
                if(!saveDirFile.exists()){
                    saveDirFile.mkdirs();
                }
                //关联要上传的文件
                File uploadFile = new File(saveDirFile, fileName);
                item.write(uploadFile);//实现文件上传
                //上传完文件后，把文件名称添加到news对象中
                news.setPicPath(fileName);
            }
        }
    }
    news.setCreateDate(new Date());
    //接收用户从前端填写的参数
    /*int categoryId = Integer.parseInt(request.getParameter("categoryId"));
    String title = request.getParameter("title");
    String author = request.getParameter("author");
    String summary = request.getParameter("summary");
    String content = request.getParameter("content");
    String picPath = request.getParameter("picPath");
    //基于上面的参数创建新闻对象
    news=new News(categoryId,title,summary,content,author,picPath,new Date());*/
    //新闻添加成功后跳转到列表页
    int count=NewsService.addNews(news);
    if(count>0){


%>
<jsp:forward page="newsDetailList.jsp"></jsp:forward>
<%
    }
%>